package com.cai.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cai.entity.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.cai.dao.MemberDao;
import com.cai.dao.OrderDao;
import com.cai.dao.OrderSettingDao;
import com.cai.dao.OrderSettingListDao;
import com.cai.entity.PageResult;
import com.cai.entity.QueryPageBean;
import com.cai.pojo.Member;
import com.cai.pojo.Order;
import com.cai.pojo.OrderSetting;
import com.cai.pojo.OrderSettingList;
import com.cai.service.OrderSettingListService;
import com.cai.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {

    @Autowired
    private OrderSettingListDao orderSettingListDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 分页
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) throws Exception {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<OrderSettingList> page = orderSettingListDao.findByCondition(queryPageBean.getQueryString());
        //改变日期格式
        // 2019-04-28 00:00:00 --> 2019年4月28日
        List<OrderSettingList> result = page.getResult();
        for (OrderSettingList orderSettingList : result) {
            Date orderDate = orderSettingList.getOrderDate();
            String sDate = DateUtils.parseDate2String1(orderDate);
            orderSettingList.setStrOrderDate(sDate);
        }
        return new PageResult(page.getTotal(), result);
    }

    /**
     * 添加预约
     *
     * @param orderSettingList
     * @param setmealIds
     */
    @Override
    public Result add(OrderSettingList orderSettingList, Integer[] setmealIds) throws Exception {
        /**
         * IdCard: "123456"
         * age: "35"
         * name: "王小波"
         * orderDate: "2019-10-22"
         * phoneNumber: "19912345678"
         * sex: "1"
         */
        //用PhoneNum判断是否是会员
        String phoneNumber = orderSettingList.getPhoneNumber();
        Member member = memberDao.findByTelephone(phoneNumber);
        //Integer count = orderDao.findCount();
        Integer count1 = memberDao.findMemberCountByDate(DateUtils.parseDate2String(DateUtils.getToday()));
        count1 += 11;
        if (member == null) {      //不是会员---->变成会员//不管预约成不成功先加个会员
            member = new Member();
            member.setName(orderSettingList.getName());
            member.setPhoneNumber(phoneNumber);
            member.setSex(orderSettingList.getSex());
            //档案号 时间戳+今日新增会员数会员
            member.setFileNumber(DateUtils.parseDate2String(new Date(), "yyyyMMdd") + count1.toString());
            if (orderSettingList.getIdCard() != null) {
                member.setIdCard(orderSettingList.getIdCard());
            }
            member.setRegTime(DateUtils.getToday());
            memberDao.add(member);
        }
        //判断日期是否能够预约
        Integer number = orderSettingDao.findNumberByDate(orderSettingList.getOrderDate());
        Integer reservation = orderSettingDao.findReservationByDate(orderSettingList.getOrderDate());
        if (number != null && number > reservation) {       //可预约
            //用会员信息添加预约
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderSettingList.getOrderDate());
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setOrderType(Order.ORDERTYPE_TELEPHONE);
            if (setmealIds != null && setmealIds.length > 0) {      //同时预约一个或多个套餐
                for (Integer setmealId : setmealIds) {
                    order.setSetmealId(setmealId);
                    System.out.println(order);
                    orderDao.add(order);
                    //预约成功 ----> orderSetting +1
                    OrderSetting orderSetting = new OrderSetting();
                    orderSetting.setOrderDate(orderSettingList.getOrderDate());
                    reservation++;
                    orderSetting.setReservations(reservation);
                    orderSettingDao.editReservationsByOrderDate(orderSetting);

                }
            }
            return new Result(true, "预约成功！");
        } else {
            return new Result(false, "预约失败，请更换日期再试");
        }

    }

    /**
     * 删除预约数据
     *
     * @param id
     */
    @Override
    public void del(Integer id) {
        Date date = orderDao.findDateById(id);
        orderDao.del(id);
        Integer reservation = orderSettingDao.findReservationByDate(date);
        reservation--;
        OrderSetting orderSetting = new OrderSetting();
        orderSetting.setOrderDate(date);
        orderSetting.setReservations(reservation);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
    }
}
